package main

import (
	"duanju-mq/db"
	"duanju-mq/mq"
	"duanju-mq/pkg/app"
	"duanju-mq/pkg/json"
	"duanju-mq/pkg/log"
	"duanju-mq/pkg/redis"
	"duanju-mq/pkg/util/convert"
	"duanju-mq/pkg/worker"
	"time"

	_ "duanju-mq/cron"
	_ "duanju-mq/pkg/log/filelog"
	_ "duanju-mq/pkg/systemd/autostart"
)

var (
	//rqworker        = worker.NewDispatcher(doquestData)
	//reportApiWorker = worker.NewDispatcher(doReportApiData)
	//requestDataMq   = "ruestData"
	//reportApiMq     = "reportApi"

	duanjuApiWorker = worker.NewDispatcher(doDuanjuApiData)
	duanjuApiMq     = "duanjuApi"
)

func init() {
	mq.Init(duanjuApiMq)
	db.AfterDbOpen(func() {
		app.Async(loopDuanjuApiData)
	})
}

func main() {
	app.Start()
	app.Block()
}

func loopDuanjuApiData() {
ForEnd:
	for {
		select {
		case <-app.CTX.Done():
			break ForEnd
		default:
			checkDuanjuApiData()
		}
		time.Sleep(time.Second)
	}
}

func checkDuanjuApiData() {
	for {
		rawData, err := mq.RecvMsg(duanjuApiMq, 0)
		if err != nil {
			if err != redis.ErrNil {
				log.Err("task mq err:", err)
			}
			break
		}
		baseData := &DuanjuApiBaseData{}
		json.Unmarshal([]byte(rawData), baseData)
		baseData.RawData = rawData
		duanjuApiWorker.Handle(convert.ToString(baseData.Uid), baseData)
	}
}
